﻿using System;
using System.Drawing;

#pragma warning disable IDE0130 // 命名空间与文件夹结构不匹配
namespace zykControl
#pragma warning restore IDE0130 // 命名空间与文件夹结构不匹配
{
    /// <summary>
    /// 持有对象的文本框
    /// </summary>
    [System.ComponentModel.DefaultEvent("KeyEnterObject")]
    public class EditObject : EditBase
    {
        private object? mObject;
        private string mText = "";
        /// <summary>
        /// 当在文本框中回车,并且文本框内包含数据时发生
        /// </summary>
        /// <remarks></remarks>
        [System.ComponentModel.Description("当在文本框中回车,并且文本框内包含数据时发生")]
        public event KeyEnterObjectEventHandler? KeyEnterObject;

        /// <summary>
        /// 当在文本框中回车,并且文本框内包含数据时发生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public delegate void KeyEnterObjectEventHandler(object sender, TxtItemFindDateArgs e);
        /// <summary>
        /// 持有对象的文本框
        /// </summary>
        public EditObject()
        {
            Enter += EditBoxObject_GotFocus;
            Leave += EditBoxObject_LostFocus;
            DoubleClick += new EventHandler(EditObject_DoubleClick);
            if (DesignMode == true) BackColor = Color.GreenYellow;
            OnKeyEnter = EditBoxObject_KeyEnter;
        }

        private void EditBoxObject_GotFocus(object? sender, EventArgs? e)
        {
            Text = "";
        }

        private bool EditBoxObject_KeyEnter()
        {
            var dArgs = new TxtItemFindDateArgs(Text, true);
            if (Text != "" && KeyEnterObject != null)
            {
                KeyEnterObject?.Invoke(this, dArgs);
                return dArgs.ToNextFocus;
            }
            else
            {
                return true;
            }
        }

        private void EditBoxObject_LostFocus(object? sender, EventArgs? e)
        {
            Text = mText;
        }

        /// <summary>
        /// 为对象式文本框设置对象
        /// </summary>
        /// <param name="theObject"></param>
        /// <param name="theDisplayText"></param>
        /// <remarks></remarks>
        public void SetObject(object theObject, string theDisplayText)
        {
            mObject = theObject;
            mText = theDisplayText;
        }

        /// <summary>
        /// 返回此文本框包含的对象
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        public object? GetObject()
        {
            return mObject;
        }

        /// <summary>
        /// 如果此文本框中的对象为Nothing,返回True
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        public bool IsNothing()
        {
            return mObject is null;
        }

        /// <summary>
        /// 重写基类的OnCreateControl,使控件被创建时,Text属性设置为对象的展示文本
        /// </summary>
        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            if (DesignMode == false) Text = mText;
        }

        /// <summary>
        /// 重写基类的OnTextChanged, 使控件无论设置为什么,在设计模式下都不会显示内容
        /// </summary>
        /// <param name="e"></param>
        protected override void OnTextChanged(EventArgs e)
        {
            if (DesignMode == true) Text = "";
            base.OnTextChanged(e);
        }

        private void EditObject_DoubleClick(object? sender, EventArgs? e)
        {
            KeyEnterObject?.Invoke(this, new TxtItemFindDateArgs("", false));
        }
    }
}